#include "deletemessage.h"
#include "ui_deletemessage.h"


using namespace  std;
extern QTcpSocket *client;

deleteMessage::deleteMessage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::deleteMessage)
{
    ui->setupUi(this);
    connect(ui->pushButton_back,SIGNAL(clicked()),this,SLOT(sendDeleteSignal()));
}

deleteMessage::~deleteMessage()
{
    delete ui;
}

void deleteMessage::insert_msg(QString &msg, QString s1, QString s2){
    msg.append(s1);
    msg.append(":  ");
    msg.append(root["select_result"]["nresult"][0][s2.toStdString().c_str()].asString().c_str());
    msg.append("\n");
}

void deleteMessage::on_pushButton_find_clicked()
{
    root["cmd"] = "selectEmployee_req";
    root["employeeid"] = ui->lineEdit_id->text().toStdString();

    //将员工id发送给服务器
    client->write(writer.write(root).c_str());

    //等待服务器回应是否查找成功，成功将信息显示在edit内，失败弹出查找失败原因
    client->waitForReadyRead(100);//阻塞等待read
    client->read(jsonstr, sizeof(jsonstr));
    reader.parse(jsonstr,root);
    qDebug() << root.toStyledString().c_str();
    if(strcmp(root["cmd"].asString().c_str(), "selectEmployee_OK") == 0){
        QString emp_meg;
        insert_msg( emp_meg, "姓名", "name");
        insert_msg( emp_meg, "性别", "sex");
        insert_msg( emp_meg, "手机", "phone");
        insert_msg( emp_meg, "入职时间", "time");
        insert_msg( emp_meg, "部门", "department");
        insert_msg( emp_meg, "职位", "position");
        insert_msg( emp_meg, "邮箱", "email");
        ui->textEdit_message->setText(emp_meg);
    }else{
        QMessageBox::warning(this, "注册失败", root["cmd"].asString().c_str(), QMessageBox::Yes, QMessageBox::Yes);
    }
}

void deleteMessage::on_pushButton_delete_clicked()
{
    if(QMessageBox::Yes == QMessageBox::warning(this,"警告","确定删除该员工信息？",QMessageBox::Yes | QMessageBox:: No))
    {
        root["cmd"] = "deleteEmployee_req";
        root["employeeid"] = ui->lineEdit_id->text().toStdString().c_str();
        //将员工id发送给服务器
        client->write(writer.write(root).c_str());

        //等待服务器回应是否查找成功，成功将信息显示在edit内，失败弹出查找失败原因
        client->waitForReadyRead(100);//阻塞等待read
        client->read(jsonstr, sizeof(jsonstr));
        reader.parse(jsonstr,root);
        if(strcmp(root["cmd"].asString().c_str(), "deleteEmployee_OK") == 0){
            emit deleteFaceSignal(ui->lineEdit_id->text().toStdString());
            qDebug() << "asdasdasdasdasd";
            QMessageBox::information(this, "删除成功", "员工信息已成功删除", QMessageBox::Yes, QMessageBox::Yes);
        }else{
            QMessageBox::warning(this, "删除失败", root["cmd"].asString().c_str(), QMessageBox::Yes, QMessageBox::Yes);
        }
    }
}

void deleteMessage::sendDeleteSignal(){
    this->close();
    emit deleteSignal();
}

